package java类及封装;
// Student.java（完整代码）
public class c {
    public static void main(String[] args) {
        // 用无参构造创建对象
        c stu1 = new c();
        stu1.setAge(22);
        stu1.showInfo();

        // 用有参构造创建对象
        c stu2 = new c("20250804", "赵六");
        stu2.setAge(25);
        stu2.showInfo();
    }
    private String studentId;
    private String name;
    private int age;

    // 无参构造方法（默认存在，手动定义需显式写出）
    public c() {
        this.studentId = "未知";
        this.name = "未知";
    }

    // 有参构造方法（初始化学号和姓名）
    public c(String studentId, String name) {
        this.studentId = studentId;
        this.name = name;
    }

    // getter/setter 方法
    public String getStudentId() { return studentId; }
    public void setStudentId(String studentId) { this.studentId = studentId; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public int getAge() { return age; }
    public void setAge(int age) {
        if (age >= 18 && age <= 30) {
            this.age = age;
        } else {
            System.out.println("年龄不合法（必须18-30岁）");
        }
    }

    // 自定义方法：显示学生完整信息
    public void showInfo() {
        System.out.println("学号：" + studentId + "，姓名：" + name + "，年龄：" + age + "岁");
    }
}